﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAOTicketReservationSystem
{
    public class Dao
    {
        private TicketReservationSystemDataContext context;

        private const string successMessage = "success";        
        private const string statusOrdered = "ordered";

        public Dao()
        {
            this.context = new TicketReservationSystemDataContext();
        }

        public void Save()
        {
            this.context.SubmitChanges();
        }

        public void Cancel()
        {
            this.context = new TicketReservationSystemDataContext();
        }

        public string DeleteCategoryById(int categoryId)
        {
            Category categoryToDelete = (from category in context.Categories
                                         where category.CategoryID == categoryId
                                         select category).SingleOrDefault();
            try
            {
                context.Categories.DeleteOnSubmit(categoryToDelete);
                context.SubmitChanges();
            }
            catch (Exception e)
            {
                return e.Message;
            }

            return successMessage;
        }

        public string DeleteLocationById(int locationId)
        {
            Location locationToDelete = (from location in context.Locations
                                         where location.LocationID == locationId
                                         select location).SingleOrDefault();
            try
            {
                context.Locations.DeleteOnSubmit(locationToDelete);
                context.SubmitChanges();
            }
            catch (Exception e)
            {
                return e.Message;
            }

            return successMessage;
        }

        public IQueryable<Category> GetAllCategories()
        {
            return this.context.Categories;
        }

        public IQueryable<Location> GetAllLocations()
        {
            return context.Locations;
        }

        public IQueryable<Event> GetAllEvents()
        {
            return context.Events;
        }
                
        public string DeleteEventById(int evenId)
        {
            Event eventToDelete = (from ev in context.Events
                                   where ev.EventID == evenId
                                   select ev).SingleOrDefault();
            try
            {
                context.Events.DeleteOnSubmit(eventToDelete);
                context.SubmitChanges();                
            }
            catch (Exception e)
            {
                return e.Message;
            }

            return successMessage;
        }

        public Event GetEventById(int eventId)
        {
            Event ev = (from eve in context.Events
                        where eve.EventID == eventId
                        select eve).SingleOrDefault();
            return ev;
        }

        public string AddEvent(Event eventToAdd)
        {
            try
            {
                context.Events.InsertOnSubmit(eventToAdd);
                context.SubmitChanges();
            }
            catch (Exception e)
            {
                return e.Message;
            }

            return successMessage;
        }

        public string EditEvent(Event newEvent)
        {
            Event eventToEdit = (from ev in context.Events
                                 where ev.EventID == newEvent.EventID
                                 select ev).SingleOrDefault();
            try
            {
                eventToEdit.CategoryID = newEvent.CategoryID;
                eventToEdit.LocationID = newEvent.LocationID;
                eventToEdit.EventName = newEvent.EventName;
                eventToEdit.Date = newEvent.Date;
                eventToEdit.Price = newEvent.Price;
                context.SubmitChanges();
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return successMessage;
        }

        public static IQueryable<Event> GetUpcomingEvents()
        {
            TicketReservationSystemDataContext context =
                   new TicketReservationSystemDataContext();
            var result = from ev in context.Events
                         where (ev.Date - DateTime.Now).Days > 2
                         select ev;
            return result;
        }

        public static int InsertPurchase(int eventId, string customerName, int numberOfTickets, string address)
        {
            TicketReservationSystemDataContext context =
                new TicketReservationSystemDataContext();
            Purchase purchase = new Purchase()
            {
                EventID = eventId,
                CustomerName = customerName,
                NumberOfTickets = numberOfTickets,
                Address = address,
                Status = statusOrdered,
                Date = DateTime.Now
            };

            context.Purchases.InsertOnSubmit(purchase);
            context.SubmitChanges();
            return purchase.PurchaseID;
        }

        public static void EditPurchaseStatus(int purchaseId, string newStatus)
        {
            TicketReservationSystemDataContext context =
                   new TicketReservationSystemDataContext();
            Purchase purchase = (from pr in context.Purchases
                                 where pr.PurchaseID == purchaseId
                                 select pr).SingleOrDefault();
            purchase.Status = newStatus;
            context.SubmitChanges();
        }

        public static string GetSuccessMessage()
        {
            return successMessage;
        }
    }
}
